'use strict';
// const Controller = require('egg').Controller;
const nodemailer = require("nodemailer")
const Controller = require('egg').Controller;

class yjController extends Controller {
	//汽车查询
    async Cardetail() {
       // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  ORDER  BY carid  DESC  LIMIT 9`   
       this.ctx.body=await this.app.mysql.query(sql)
    }
	//汽车查询
	async sousuocar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//企业汽车查询
	async qiyecar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  WHERE VehicleType="大巴车" OR VehicleType="商务车"  ORDER  BY carid  DESC`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//个人汽车查询
	async gerencar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  WHERE VehicleType="轿车"`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//接机汽车查询
	async jiejicar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  WHERE VehicleType="商务车"`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//自选汽车查询
	async zixuancar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  WHERE VehicleType="跑车" OR VehicleType="豪车"  ORDER  BY carid  DESC`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//婚车汽车查询
	async hunchecar() {
	   // console.log(999999)
	   let sql = `SELECT  * FROM  carinformation  WHERE VehicleType="婚车"`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//具体汽车
	async cartype() {
	   // console.log(888)
	   let query=this.ctx.query
	   let sql = `SELECT  * FROM  carinformation WHERE carid  LIKE ${query.id};`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
    //新闻查询
	async news() {
	   console.log("news")
	   // let query=this.ctx.query
	   let sql = `SELECT  * FROM  news WHERE type='行业动态' ORDER  BY newsID DESC ;`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//新闻查询industry
	async industry() {
	   console.log("industry")
	   let query=this.ctx.query
	   console.log(query)
	   let sql = `SELECT  * FROM  news WHERE type='公司新闻' ORDER  BY newsID DESC  ;`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//具体汽车
	async newsdetail() {
	   // console.log(888)
	   let query=this.ctx.query
	   let sql = `SELECT  * FROM  news WHERE newsID  LIKE ${query.id};`   
	   this.ctx.body=await this.app.mysql.query(sql)
	}
	//预定
	async yudin() {
	   // console.log(888)
	   console.log(this.ctx.session.email,33333) 
	   let query=this.ctx.query
	   console.log(query)
	   // let sql = `SELECT  * FROM  news WHERE newsID  LIKE ${query.id};`
		  if(this.ctx.session.email){
			   this.ctx.body={code:200}
		  }else{
			  this.ctx.body={code:100}
		  }
	   
	}
	//预定内容
	async yudincontent() {
     console.log(123456)
	   const {
	       ctx
	   } = this;
	   // console.log(666666);
	   var ziduan = ctx.request.body
	   console.log(ziduan.email);
	   //将数字、小写字母及大写字母输入
	   var str = "1234567890";
	   //给一个空字符串
	   var res = '';
	   //循环4次，得到4个字符
	   for (var i = 0; i < 5; i++) {
	       //将得到的结果给字符串，调用随机函数，0最小数，62表示数字加字母的总数
	       res += str[Math.floor(Math.random() * 10)];
	   }
	   console.log(res);
	   
	   var yzm = res
	   let data = {
	       email: ziduan.email, // 接收者的邮箱
	       // 邮件模板，可自行修改
	       content: `
	       <head>
	       <base target="_blank" />
	       <style type="text/css">::-webkit-scrollbar{ display: none; }</style>
	       <style id="cloudAttachStyle" type="text/css">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>
	       <style id="blockquoteStyle" type="text/css">blockquote{display:none;}</style>
	       <style type="text/css">
	           body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}
	           td, input, button, select, body{font-family:Helvetica, 'Microsoft demo', verdana}
	           pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}
	           th,td{font-family:arial,verdana,sans-serif;line-height:1.666}
	           img{ border:0}
	           header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}
	           blockquote{margin-right:0px}
	           </style>
	       </head>
	       <body tabindex="0" role="listitem">
	       <table width="700" border="0" align="center" cellspacing="0" style="width:700px;">
	           <tbody>
	           <tr>
	               <td>
	                   <div style="width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;">
	                       <table border="0" cellpadding="0" cellspacing="0" width="700" height="39" style="font:12px Tahoma, Arial, 宋体;">
	                           <tbody><tr><td width="210"></td></tr></tbody>
	                       </table>
	                   </div>
	                   <div style="width:680px;padding:0 10px;margin:0 auto;">
	                       <div style="line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;">
	                           <strong style="display:block;margin-bottom:15px;">尊敬的管理员用户：<span style="color:#f60;font-size: 16px;"></span>您好！</strong>
	                           <strong style="display:block;margin-bottom:15px;">
	                               您正在进行<span style="color: orange">蓉之风租车订单确认</span>操作，请确认以下租车信息并及时联系客户。
	                           </strong>
								<br/>
								<h1 align="center" style="color:orange;">订单信息确认</h1>
								<table style="text-align: center;" border="1px" cellspacing='0' width='500px' height='300px' align="center">
									<tr>
										<td>姓名</td>
										<td>${ziduan.name}</td>
									</tr>
									<tr>
										<td>电话</td>
										<td>${ziduan.tel}</td>
									</tr>
									<tr>
										<td>邮件</td>
										<td>${ziduan.email2}</td>
									</tr>
									<tr>
										<td>租车天数(天)</td>
										<td>${ziduan.day}</td>
									</tr>
									<tr>
										<td>租车价格(元/天)</td>
										<td>${ziduan.money}</td>
									</tr>
								</table>
	                       </div>
	                       <div style="margin-bottom:30px;">
	                           <small style="display:block;margin-bottom:20px;font-size:12px;">
	                               <p style="color:#747474;">
	                                   注意：此操作可能会修改您的密码、登录邮箱或绑定手机。如非本人操作，请及时登录并修改密码以保证帐户安全
	                                   <br>（工作人员不会向你索取此验证码，请勿泄漏！)
	                               </p>
	                           </small>
	                       </div>
	                   </div>
	                   <div style="width:700px;margin:0 auto;">
	                       <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;">
	                           <p>此为系统邮件，请勿回复<br>
	                               请保管好您的邮箱，避免账号被他人盗用
	                           </p>
	                           <p>Bertil Chan</p>
	                       </div>
	                   </div>
	               </td>
	           </tr>
	           </tbody>
	       </table>
	       </body>
	   `
	   }
	   let transporter = nodemailer.createTransport({
	       host: 'smtp.163.com', //163邮箱smtp服务地址
	       port: 465, // SMTP 端口
	       auth: {   //发送者的账户和授权码
	           user: 'lsy1626128667@163.com', //个人邮箱账户
	           pass: 'FIDUUYGNFDTDHWBG', //smtp授权码，到邮箱设置下获取
	       }
	   });
	   let mailOptions = {
	       from: {
	           name: '蓉之风官方平台',
	           address: 'lsy1626128667@163.com'
	       },
	       // 发送者昵称和地址
	       to: data.email, // 接收者的邮箱地址
	       subject: '订单信息确认', // 邮件主题
	       html: data.content
	   };
	   //发送邮件
	   transporter.sendMail(mailOptions, (error, info) => {
	       if (error) {
	           return console.log(error);
	       }
	       console.log('邮件发送成功 ID：', info.messageId);
	   });
	   ctx.body = {
	       info: "验证码",
	       yzm,
	       code: 2001
	   
	   }
	}
}


module.exports = yjController;
